﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Entity;

namespace DAL
{
    public class UserService
    {
        public bool UserLogin(string name, string pass)
        {
            string sql = string.Format(@"select * 
from Users where username='{0}' and userpass = '{1}'", name, pass);

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            bool hasData = dr.Read();
            dr.Close();
            conn.Close();
            return hasData;
        }

        public User GetUserByName(string name)
        {
            User u = null;
            string sql = string.Format(@"select * 
from Users where username='{0}'", name);

            SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
            SqlCommand cmd = new SqlCommand(sql, conn);
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                u = new User();
                u.UserName = dr["username"].ToString();
                u.UserPass = dr["userpass"].ToString();
            }
            dr.Close();
            conn.Close();
            return u;
        }
    }
}
